var editor = new Simditor({
    textarea: $('#editor'),
    //optional options
    toolbar:[
        'title', 'bold', 'italic', 'underline',
        'strikethrough', 'fontScale', 'color', 'ol','ul', 'blockquote', 'code',
        'table', 'link', 'image', 'hr', 'indent', 'outdent', 'alignment'
    ],
    cleanPaste:true,
    pasteImage:true,
    upload:{
        url:ctxPath+'/blog/admin/tool/api/ajaxUploadImage',
        fileKey:'myFileNames',
        connectionCount:'3',
        leaveConfirm:'图片上传中...',
        success: function(res) {

        }
    }
});
layui.use(['form','upload'], function(){
    $('.select-lang').attr('lay-ignore','');
    var form = layui.form;
    var upload = layui.upload;

    //执行实例
    var tinyUpload = upload.render({
        elem: '#uploadTinyBtn', //绑定元素
        url: ctxPath+'/blog/admin/tool/api/ajaxUploadImage',//上传接口
        field:'myFileNames',
        done: function(res){
            console.log(res);
            //上传完毕回调
            if(res.success){
                $("#uploadPrev").attr('src',ctxPath+res.file_path);
                $("input[name='tiny']").val(res.file_path);
                $("#uploadPrev").show();
            }else {
                layer.msg(res.msg,function(){});
            }
        },
        error: function(){
            //请求异常回调
            layer.msg('请求出错',function(){});
        }
    });

    // ajax加载栏目，并且更新layui的select组件
    $(function () {
       $.post(ctxPath+'/blog/admin/typecolum/api/find',{upid:-1},function (res) {
           if(res.obj!=null&&res.obj.length!=0){
               var data=res.obj;
               $('#columnSelect option').remove();
               var appendHtml='<option value=""></option>';
               for (var i=0;i<data.length;i++){
                   if(isUpdatePage&&columnId==data[i].id){
                       appendHtml+='<option value="'+data[i].id+'" selected="selected">'+data[i].typename+'</option>';//修改页面的时候，默认栏目
                   }else {
                       appendHtml+='<option value="'+data[i].id+'">'+data[i].typename+'</option>';
                   }
               }
               console.log(appendHtml);
               $('#columnSelect').append(appendHtml);
               form.render('select','columnSelect');
               // form.render('select');
           }
       },'json');
       //修改页面时，加载默认类型
        $.post(ctxPath+'/blog/admin/typecolum/api/find',{upid:columnId},function (res) {
            if(res.obj!=null&&res.obj.length!=0){
                var data=res.obj;
                $('#typeSelect option').remove();
                var appendHtml='<option value=""></option>';
                for (var i=0;i<data.length;i++){
                    if(data[i].id==typeId){
                        appendHtml+='<option value="'+data[i].id+'" selected="selected">'+data[i].typename+'</option>';
                    }else {
                        appendHtml+='<option value="'+data[i].id+'">'+data[i].typename+'</option>';
                    }
                }
                console.log(appendHtml);
                $('#typeSelect').append(appendHtml);
                form.render('select','typeSelect');
            }
        },'json');

        if(isUpdatePage){
            // editor.setValue($('#hideEditor').val());
            var $pre = $("pre[data-lang='html']");
            for (var i = 0; i < $pre.length; i++) {
                var html=$($pre[i]).html();
                var text=''+html+'';
                $($pre[i]).empty();
                $($pre[i]).text(text);
            }
        }
    });
    form.on('select(columnSelect)', function(data){
        if(data.value==null||data.value==''){
            $('#typeSelect option').remove();
            form.render('select','typeSelect');
            return;
        }
        $.post(ctxPath+'/blog/admin/typecolum/api/find',{upid:data.value},function (res) {
            if(res.obj!=null&&res.obj.length!=0){
                var data=res.obj;
                $('#typeSelect option').remove();
                var appendHtml='<option value=""></option>';
                for (var i=0;i<data.length;i++){
                    appendHtml+='<option value="'+data[i].id+'">'+data[i].typename+'</option>';
                }
                console.log(appendHtml);
                $('#typeSelect').append(appendHtml);
                form.render('select','typeSelect');
            }else {
                $('#typeSelect option').remove();
                form.render('select','typeSelect');
            }
        },'json');
    });
});
function insert(state) {
    if($.mine.formValidate('#form',2000)){
        var load = layer.load(1, {
            shade: [0.6,'#000'] //0.1透明度的白色背景
        });
        var reqData=$('#form').serialize()+'&articlestate='+state+'&content='+encodeURIComponent(editor.getValue());
        console.log(editor.getValue());
        console.log(reqData);
        $.post(ctxPath+'/blog/admin/article/api/insert',reqData,function (res) {
            layer.close(load);
            if(res.obj.id!=null&&res.obj.id!=''){
                layer.msg('操作成功',{icon:1});
                setTimeout(function () {
                    var  frameindex= parent.layer.getFrameIndex(window.name);
                    parent.layer.close(frameindex);
                },1000);
            }else {
                layer.msg('操作失败',{icon:2});
            }
        },'json');
        setTimeout(function () {
            layer.close(load);
            layer.msg('出现错误！',function(){});
        },5000);
    }
}
function update() {
    if($.mine.formValidate('#form',2000)){
        var load = layer.load(1, {
            shade: [0.6,'#000'] //0.1透明度的白色背景
        });
        var reqData=$('#form').serialize()+'&content='+encodeURIComponent(editor.getValue());
        console.log(reqData);
        $.post(ctxPath+'/blog/admin/article/api/update',reqData,function (res) {
            layer.close(load);
            if(res.resNum==1){
                layer.msg('操作成功',{icon:1});
                setTimeout(function () {
                    var  frameindex= parent.layer.getFrameIndex(window.name);
                    parent.layer.close(frameindex);
                },1000);
            }else {
                layer.msg('操作失败',{icon:2});
            }
        },'json');
        setTimeout(function () {
            layer.close(load);
            layer.msg('出现错误！',function(){});
        },5000);
    }
}